<?php

namespace app\api\controller\yalimedia;

use addons\yalimedia\library\QRcode;
use app\common\controller\Api;

/**
 * 分享底图接口
 */
class Shareimg extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['index'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 获取分享底图列表
     */
    public function index()
    {
        $page = $this->request->post('page');
        $limit = $this->request->post('limit');

        $subject = new \addons\yalimedia\service\Shareimg();
        $data = $subject->getShareimgList($page,$limit);
        foreach($data['data'] as &$v){
            $v['small_img'] = $this->request->domain().$v['small_img'];
            $v['image'] = $this->request->domain().$v['image'];
        }
        $this->success('请求成功', $data);
    }

    public function share_img(){
        $course_id = $this->request->post('course_id');
        $path = $this->request->post('path',"");
        $img_id = $this->request->post('img_id');

        $courseModel = new \app\admin\model\yalimedia\Course();
        $course = $courseModel->where(['id'=>$course_id,'status'=>1])->find();
        if(empty($course)){
            $this->error("当前课程不存在");
        }

        $shareImgModel = new \app\admin\model\yalimedia\Shareimg();
        $shareImg = $shareImgModel->where(['id'=>$img_id])->find();
        if(empty($shareImg)){
            $this->error("当前图片不存在");
        }

        //生成二维码图片
        $errorCorrectionLevel = 'H';  //容错级别
        $matrixPointSize = 5.61;      //生成图片大小
        if(!file_exists("./assets/addons/yalimedia/qr")){
            mkdir("./assets/addons/yalimedia/qr",0777,true);
        }
        $qr_path =  "./assets/addons/yalimedia/qr/".$course['id'].".png";
        QRcode::png($this->request->domain().$path, $qr_path, $errorCorrectionLevel, $matrixPointSize, 1);

        //获取底图，生成画图工具
        $dst_path = "./".$shareImg['image'];
        $bg = imagecreatefromstring(file_get_contents($dst_path));
        $bg_width = imagesx($bg);       //底图图片宽度
        $bg_height = imagesy($bg);      //底图图片高度
        $drawTool = imagecreatetruecolor($bg_width, $bg_height);

        //重新组合图片并调整大小
        imagecopyresampled($drawTool, $bg, 0, 0, 0, 0, $bg_width,
            $bg_height, $bg_width, $bg_height);

        //商品图片
        $store_img = imagecreatefromstring(file_get_contents("./".$course['cover']));
        $store_img_width = imagesx($store_img);//二维码图片宽度
        $store_img_height = imagesy($store_img);//二维码图片高度
        //合并
        imagecopyresampled($drawTool, $store_img, 90, 220, 0, 0, 452,
            254, $store_img_width, $store_img_height);



        //课程名称
        $black = imagecolorallocate($drawTool, 0, 0, 0);
        imagettftext($drawTool, 16, 0, 90, 520, $black, getcwd() . "/assets/addons/yalimedia/song.ttf", $course['title']);


        //右下角二维码
        $logo = imagecreatefrompng($qr_path);
        $logo_width = imagesx($logo);//logo图片宽度
        $logo_height = imagesy($logo);//logo图片高度
        $logo_qr_width = 160; //logo图片在二维码图片中宽度大小
        $from_width = ($bg_width - $logo_qr_width - 100);
        $from_height = ($bg_height - $logo_qr_width - 70);

        //重新组合图片并调整大小
        imagecopyresampled($drawTool, $logo, $from_width, $from_height, 0, 0, $logo_qr_width,
            $logo_qr_width, $logo_width, $logo_height);

        if(!file_exists("./assets/addons/yalimedia/ads")){
            mkdir("./assets/addons/yalimedia/ads",0777,true);
        }

        $filename = "./assets/addons/yalimedia/ads/".$course_id."_".$img_id."_".$this->auth->id.".jpg";
        imagejpeg($drawTool, $filename);
        $fileurl = $this->request->domain()."/assets/addons/yalimedia/ads/".$course_id."_".$img_id."_".$this->auth->id.".jpg";
        imagedestroy($drawTool);
        $this->success('请求成功',array('filename' => $fileurl));
    }

}
